<?php
include_once '../conn.php';

// 处理删除请求
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $gameId = $_GET['id'];

    // 查询游戏信息以获取图片路径
    $queryGame = "SELECT image_path FROM games WHERE game_id = $gameId";
    $resultGame = $conn->query($queryGame);

    if ($resultGame->num_rows > 0) {
        $rowGame = $resultGame->fetch_assoc();
 
        // 删除数据库中的记录
        $deleteQuery = "DELETE FROM games WHERE game_id = $gameId";
        if ($conn->query($deleteQuery) === TRUE) {
            // 删除成功后删除对应的图片文件
            $imagePath = $rowGame['image_path'];
            unlink($imagePath);

            echo '<script>alert("删除成功"); window.location.href = "game.php";</script>';

        } else {
            echo "<div class='container'>";
            echo "<div class='alert alert-danger' role='alert'>Error: " . $deleteQuery . "<br>" . $conn->error . "</div>";
            echo "</div>";
        }
    } else {
        echo "<div class='container'>";
        echo "<div class='alert alert-danger' role='alert'>游戏不存在</div>";
        echo "</div>";
    }
} else {
    echo "<div class='container'>";
    echo "<div class='alert alert-danger' role='alert'>无效的游戏 ID</div>";
    echo "</div>";
}

$conn->close();
?>
